package com.kaiyidiannao.db;

import android.content.ContentValues;

import java.util.Date;

public class RpcCacheDBRecord {
	private static final String _KEY = "key";
	private static final String _BODY = "body";
	private static final String _TIME = "time";
	private String key;
	private String body;
	private String time;
	
	public RpcCacheDBRecord() {
		super();
	}
	
	public RpcCacheDBRecord(String key, String body) {
		this.key = key;
		this.body = body;
	}
	
	public RpcCacheDBRecord(String key, String body, String time) {
		this(key, body);
		this.time = "" + new Date().getTime();
	}
	
	public String getKey() {
		return key;
	}
	public void setKey(String key) {
		this.key = key;
	}
	public String getBody() {
		return body;
	}
	public void setBody(String body) {
		this.body = body;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	
	public ContentValues convert2ContentValues() {
		ContentValues cv = new ContentValues();
		cv.put(_KEY, key);
		cv.put(_BODY, body);
		cv.put(_TIME, time);
		return cv;
	}
	
	public static ContentValues convert2ContentValues(RpcCacheDBRecord record) {
		if(record == null) {
			return null;
		}
		ContentValues cv = new ContentValues();
		cv.put(_KEY, record.getKey());
		cv.put(_BODY, record.getBody());
		cv.put(_TIME, record.getTime());
		return cv;
	}
	
	public static RpcCacheDBRecord buildFromContentValues(ContentValues cv) {
		if(cv == null) {
			return null;
		}
		RpcCacheDBRecord record = new RpcCacheDBRecord();
		record.setKey(cv.getAsString(_KEY));
		record.setBody(cv.getAsString(_BODY));
		record.setTime(cv.getAsString(_TIME));
		return record;
	}
}
